
###################################################
#
#               Code to replicate 
#  The Influence Gap: Unequal policy responsiveness 
#              to men and women 
#   
#    Ruben Berge Mathisen (University of Bergen)
#
#       Tables and Figures for Manuscript
#                April 2023
#
#   Original analyses conducted in R version 
#             4.1.3 (2022-03-10)
#
##################################################

#####################################################
######## Packages and data prep #####################
#####################################################

library(readxl) # reading excel files
library(dplyr) # data manipulations
library(ggplot2) # data viz
library(ggeffects) # marginal effects plots
library(interplot) # interaction plots
library(gridExtra) # having multiple plots in a grid
library(plyr) # more data manipulations?
library(kableExtra) # making tables
library(stargazer) # regression tables
library(interflex) # flexible interaction models
library(tidyr) # reshaping data
library(car) # regression diagnostics
library(RColorBrewer) # more color palettes
library(scales) # scaling functions in plots

# logit transformation
logitTransform <- function(p) { log(p/(1-p)) }

# Main datasets 
d_us <- read_excel("Main dataset for United States, from Gilens 2012.xlsx")
d_us$country <- "United States" # adds a country column to the Gilens data

d_no <- read_excel("Main dataset for Norway.xlsx")
d_no$country <- "Norway" # adds a norway column

# Data on women in parliament
parl <- read_excel("Share of women MPs norway and us.xlsx")

## Fill in data for non-election years, Norway
parl_nor <- rbind.fill(parl%>%filter(country=="norway"),
                       parl%>%filter(country=="norway")%>%mutate(year=year+1),
                       parl%>%filter(country=="norway")%>%mutate(year=year+2),
                       parl%>%filter(country=="norway")%>%mutate(year=year+3))

## Make lagged versions of variable and 4-year averages, Norway
parl_nor <- parl_nor %>% 
  arrange(year) %>%
  mutate(share_t1=lead(share,1),
         share_t2=lead(share,2),
         share_t3=lead(share,3),
         share_t4=lead(share,4),
         share_avg4=(share+share_t1+share_t2+share_t3+share_t4)/5)

## Fill in data for non-election years, US
parl_us <- rbind.fill(parl%>%filter(country=="us"),
                      parl%>%filter(country=="us")%>%mutate(year=year+1))

## Make lagged versions of variable and 4-year averages, US
parl_us <- parl_us %>% 
  arrange(year) %>%
  mutate(share_t1=lead(share,1),
         share_t2=lead(share,2),
         share_t3=lead(share,3),
         share_t4=lead(share,4),
         share_avg4=(share+share_t1+share_t2+share_t3+share_t4)/5)

# combine Norwegian and US parliament data
parl_full <- rbind(parl_nor,parl_us)

# more lagged data by 1 year
parl_full <- parl_full %>% 
  dplyr::group_by(country) %>%
  arrange(year) %>%
  mutate(share_ld1=lead(share,1)) %>%
  ungroup()

# Data on women in top 1 percent of the income distribution
inc <- read_excel("Womens share of top 1 income group.xlsx")
inc <- inc %>% 
  dplyr::group_by(country) %>%
  dplyr::arrange(year) %>%
  dplyr::mutate(share_t1=lead(share,1),
                share_t2=lead(share,2),
                share_t3=lead(share,3),
                share_t4=lead(share,4),
                share_avg4=(share+share_t1+share_t2+share_t3+share_t4)/5)

# Combine US and Norwegian main datasets
d <- rbind.fill(d_us,d_no)

# Add data on women in parliament to the main dataframe d
d <- merge(d,parl_full%>%
             dplyr::rename(women_share=share_avg4,
                           YEAR=year)%>%
             mutate(country=dplyr::recode(country,"us"="United States",
                                          "norway"="Norway")),
           by=c("country","YEAR"))

# Add data on share of women in top 1 percent to the main dataframe d
d <- merge(d,inc %>%
             dplyr::rename(top1_share=share_avg4,
                           YEAR=year)%>%
             mutate(country=dplyr::recode(country,"us"="United States",
                                          "norway"="Norway")),
           by=c("country","YEAR"),all.x = T)

# New main variables
d$outcome <- d$OUTCOME
d$all <- d$PREDALL_SW # predicted value for all voters

# Calc preferences for males and use logit transformation for odds-ratio
d$male <- d$MALE_FAV/(d$MALE_FAV+d$MALE_OPP)
d$male <- ifelse(d$SWITCHER==1,1-d$male,d$male)
d$male_logit <- logitTransform(d$male)

# same with females
d$female <- d$FEMALE_FAV/(d$FEMALE_FAV+d$FEMALE_OPP)
d$female <- ifelse(d$SWITCHER==1,1-d$female,d$female)
d$female_logit <- logitTransform(d$female)

# recode the outcome to binary
d$outcome <- car::recode(d$outcome, "0=0; 1=1;2=1;3=1;4=1; else=NA") # Make US outcome variable a dummy, like Norwegian one

# logit transformation for income variables
d$inc90_logit<-logitTransform(d$PRED90_SW)
d$inc50_logit<-logitTransform(d$PRED50_SW)
d$inc10_logit<-logitTransform(d$PRED10_SW)
d$all_logit<-logitTransform(d$PREDALL_SW)

# reverses values for some reason
rec <- function(x) {
  x <- ifelse(d$SWITCHER==1,(1-x),x)
}

# reversing income related variables
d <- d %>% mutate_at(vars(pred_i90_men:pred_i10_women),funs(rec))
d <- d %>% mutate_at(vars(pred_i90_men_int:pred_i10_women_int),funs(rec))

# Distance variables for preference and incomes
d$genderdiff <- abs(d$male-d$female)
d$genderdiff_nonabs <- d$male-d$female
d$genderdiff_nonabs_i10 <- d$pred_i10_men-d$pred_i10_women
d$genderdiff_nonabs_i50 <- d$pred_i50_men-d$pred_i50_women
d$genderdiff_nonabs_i90 <- d$pred_i90_men-d$pred_i90_women

d$inc9050_diff <- abs(d$PRED90_SW-d$PRED50_SW)
d$inc9010_diff <- abs(d$PRED90_SW-d$PRED10_SW)
d$inc5010_diff <- abs(d$PRED50_SW-d$PRED10_SW)
d$inc9050_diff_nonabs <- d$PRED90_SW-d$PRED50_SW
d$inc9010_diff_nonabs <- d$PRED90_SW-d$PRED10_SW
d$inc5010_diff_nonabs <- d$PRED50_SW-d$PRED10_SW

# Mean centering variables 
d$all_mean <- d$all-mean(d$all,na.rm=T)
d$YEAR_mean <- d$YEAR-mean(d$YEAR,na.rm=T)
d$women_share_mean <- d$women_share-mean(d$women_share,na.rm=T)

# Gender diff, DK (don't know) included
d$male_withDK <- d$MALE_FAV/(d$MALE_FAV+d$MALE_OPP+d$MALE_DK)
d$male_withDK <- ifelse(d$SWITCHER==1,1-d$male_withDK,d$male_withDK)
d$female_withDK <- d$FEMALE_FAV/(d$FEMALE_FAV+d$FEMALE_OPP+d$FEMALE_DK)
d$female_withDK <- ifelse(d$SWITCHER==1,1-d$female_withDK,d$female_withDK)
d$genderdiff_nonabs_withDK <- d$male_withDK-d$female_withDK


#####################################################
######## Functions ##################################
#####################################################

# Functions for calulcating table and figure quantities
ests <- NULL
eff<-function(x){
  m<-lm(outcome~x,data=d1)
  ests <- data.frame(rbind(ests,round(summary(m)$coefficients[2,1],2)))
  p <- summary(m)$coefficients[2,4]
  se <- round(summary(m)$coefficients[2,2],2)
  p1 <- car::recode(p,"lo:0.009999='***'; 0.01:0.049999='**'; 0.05:0.099999='*'; 0.1:hi=''")
  result <- paste0(ests, p1)
} 
eff_se<-function(x){
  m<-lm(outcome~x,data=d1)
  ests <- data.frame(rbind(ests,round(summary(m)$coefficients[2,1],2)))
  se <- round(summary(m)$coefficients[2,2],2)
  result <- paste0(ests,"\n(",se,")")
} 
eff_num<-function(x){
  m<-lm(outcome~x,data=d1)
  ests <- data.frame(rbind(ests,round(summary(m)$coefficients[2,1],2)))
  ests
} 
se<-function(x){
  m<-lm(outcome~x,data=d1)
  se <- round(summary(m)$coefficients[2,2],2)
  se
} 
p<-function(x){
  m<-lm(outcome~x,data=d1)
  p <- summary(m)$coefficients[2,4]
  substring(as.character(ifelse(p<0.001,"<<.001", round(p,3))),2)
} 
sup20<-function(x){
  m<-lm(outcome~x,data=d1)
  round(predict(m,newdata=data.frame(x=0.2),type="response"),2)
} 
sup80<-function(x){
  m<-lm(outcome~x,data=d1)
  round(predict(m,newdata=data.frame(x=0.8),type="response"),2)
} 
fact<-function(x){
  m<-lm(outcome~x,data=d1)
  diff<- predict(m,newdata=data.frame(x=0.8),type="response")/predict(m,newdata=data.frame(x=0.2),type="response")
  round(diff,1)
} 
n<-function(x){
  m<-lm(outcome~x,data=d1)
  length(m$residuals)
} 


#####################################################
##### Tables and Figures (same order as in paper) ###
#####################################################


# Figure 1: Share of women in parliament (US House of Representatives and the Norwegian Storting)
parl$points <- ifelse(parl$country=="OECD",NA,parl$share)
ggplot(parl,aes(year,share,group=country)) +
  geom_line() +
  geom_point(aes(year,points,group=country)) +
  theme_bw() +
  geom_text(aes(label=lab),vjust=-1,hjust=0,size=3) +
  theme(panel.grid = element_blank(),
        legend.position = "none") +
  annotate("text",x=2000,y=0.45,label="Norway",size=3) +
  annotate("text",x=2007,y=0.08,label="United States",size=3) +
  annotate("text",x=1997,y=0.25,label="OECD Average",size=3) +
  scale_y_continuous(limits=c(0,0.6),breaks=seq(0,0.6,0.1), labels=function(x)paste0(x*100,"%")) +
  scale_x_continuous(breaks=seq(1940,2020,10)) +
  coord_cartesian(xlim=c(1945,2022)) +
  labs(x=NULL,y=NULL,group=NULL) +
  geom_hline(yintercept=0.5,linetype="dashed",alpha=0.3)


# Figure 2: Gender composition of the top 1 percent of the income distribution
inc2 <- inc
inc$gender <- "Women"
inc2$gender <- "Men"
inc2$share <- 1-inc2$share
inc <- rbind(inc,inc2)
inc$country <- factor(inc$country, levels(factor(inc$country))[c(2,1)])
ggplot(inc,aes(year,share,fill=gender)) +
  geom_area() +
  theme_bw() +
  scale_fill_manual(values=c("grey30","grey70")) +
  annotate("text",x=2005,y=0.21,label="Men",size=4,color="grey90") +
  annotate("text",x=2005,y=0.07,label="Women",size=4,color="grey10") +
  theme(panel.grid = element_blank(),
        legend.position = "none") +
  scale_y_continuous(limits=c(0,1),breaks=seq(0,1,0.2), labels=function(x)paste0(x*100,"%")) +
  labs(x=NULL,y=NULL,group=NULL) +
  facet_wrap(~country,scales="free_x")


# Table 1: Average preference distance by gender and income groups
d1 <- d %>% group_by(country) %>% dplyr::summarise(sex=round(mean(genderdiff,na.rm=T),3)*100,
                                                   inc50=round(mean(inc9050_diff,na.rm=T),3)*100,
                                                   inc10=round(mean(inc9010_diff,na.rm=T),3)*100)
d1<-data.frame(t(d1))[-1,]
row.names(d1) <- c("Men vs. women", "Middle income (P50) vs. high income (P90)", "Low income (P10) vs. high income (P90)")

kable(d1,booktabs=T,col.names = c("Norway","United States"),align="c",caption = "Average preference distance by gender and income groups") %>%
  add_header_above(c(" "=1, "Average absolute difference 
                     in opinion (%-points)"=2)) %>%
  column_spec(1,width="1.6in") %>% 
  column_spec(2:3,width="1in") %>%
  row_spec(1,hline_after = T) %>%
  row_spec(2,hline_after = T)


# Table 2: Policy reponsiveness to men and women overall
d1 <- d %>% filter(country=="United States")
tab1<-data.frame(all=t(d1 %>% summarise_at(vars(all),funs(eff,se,p,sup20,sup80,fact,n))),
                 male=t(d1 %>% summarise_at(vars(male),funs(eff,se,p,sup20,sup80,fact,n))),
                 female=t(d1 %>% summarise_at(vars(female),funs(eff,se,p,sup20,sup80,fact,n))))
d1 <- d %>% filter(country=="Norway")
tab2<-data.frame(all=t(d1 %>% summarise_at(vars(all),funs(eff,se,p,sup20,sup80,fact,n))),
                 male=t(d1 %>% summarise_at(vars(male),funs(eff,se,p,sup20,sup80,fact,n))),
                 female=t(d1 %>% summarise_at(vars(female),funs(eff,se,p,sup20,sup80,fact,n))))
tab <- cbind(tab1,tab2)
row.names(tab) <- c("Effect (regression coefficient)","Standard error","P-value", "Predicted probability of policy change at 20% support","Predicted probability of policy change at 80% support","Relative change in probability","Observations")
kable(tab,booktabs=T, align="c",linesep = '\\addlinespace', col.names = c("All", "Men","Women","All", "Men","Women"),
      caption = "Policy responsiveness to men and women overall") %>%
  column_spec(1,width="2.3in") %>% 
  column_spec(2:7,width="0.5in") %>% 
  row_spec(3,hline_after = T) %>%
  row_spec(6,hline_after = T) %>%
  add_header_above(c(" "=1,"United States"=3,"Norway"=3))  %>%
  footnote("Linear probability models. The observations are proposed policy changes asked about in nationally representative surveys of the US population in the period 1964-2006 (Gilens 2012), and the Norwegian population in the period 1966-2014. The dependent variable is a dichotomous measure of whether or not the policy change was adopted within four years of the time of the survey question. *p<0.1; **p<0.05; ***p<0.01",threeparttable = T) 


# Table 3: Association between preference difference between men and women and probability of policy adoption (gender gap in responsiveness)
# Models
## United States

### Chris Added this portion
d1 <- d %>% filter(country=="United States")

final_merged <- d1 %>%
  # First, join using start_year
  left_join(final_result, by = c("YEAR" = "start_year")) %>%
  # Then join using second_session_year
  left_join(final_result, by = c("YEAR" = "second_year"), suffix = c("_start", "_second")) %>%
  # Use coalesce to pick non-NA values from both joins
  mutate(
    pres_control = coalesce(pres_control_start, pres_control_second),
    house_control = coalesce(house_control_start, house_control_second),
    senate_control = coalesce(senate_control_start, senate_control_second),
    house_demcount = coalesce(house_demcount_start, house_demcount_second),
    house_repcount = coalesce(house_repcount_start, house_repcount_second),
    senate_demcount = coalesce(senate_demcount_start, senate_demcount_second),
    senate_repcount = coalesce(senate_repcount_start, senate_repcount_second)
  ) %>%
  # Remove extra columns from the two joins
  select(-contains("_start"), -contains("_second"))

### Original code
m1 <- lm(outcome~all,data=d1)
m2 <- lm(outcome~all+genderdiff_nonabs,data=d1)

### Fun New Models
# Just house control
m7 <- lm(outcome ~ all + house_control, final_merged)
summary(m7)
# House control as a control control
m8 <- lm(outcome ~ all + genderdiff_nonabs + house_control, final_merged)
summary(m8)
# House control interacted w gender diff
m9 <- lm(outcome ~ all + genderdiff_nonabs:house_control + 
           house_control + genderdiff_nonabs, final_merged)
summary(m9)

# Unified Control Analysis
final_merged <- final_merged %>%
  mutate(
    gov_control = case_when(
      house_control == "D" & senate_control == "D" ~ "Democrat",
      house_control == "R" & senate_control == "R" ~ "Republican",
      TRUE ~ "Divided"
    )
  )

m10 <- lm(outcome ~ all + genderdiff_nonabs*gov_control, final_merged)
summary(m10)

# With the president this time
final_merged <- final_merged %>%
  mutate(
    unified_gov = case_when(
      house_control == "D" & senate_control == "D" & pres_control == "D" ~ "Democrat",
      house_control == "R" & senate_control == "R" & pres_control == "R" ~ "Republican",
      TRUE ~ "Divided"
    )
  )

m11 <- lm(outcome ~ all + genderdiff_nonabs*unified_gov, final_merged)
summary(m11)


# Our Table 1: Main Hypothesis is Null

stargazer(m7,m8,m9, header=FALSE,
          omit.stat=c("f","ser","adj.rsq","AIC"),
          dep.var.caption = "DV: Policy adopted within 4 years of survey (1, 0)",
          dep.var.labels = "",
          column.labels=c("Model 1","Model 2","Model 3"),
          title = "Association between preference difference between men and women and probability of policy adoption (gender gap in responsiveness)",
          column.sep.width = "10pt",  
          notes.align = "l",
          notes.label = "",
          notes = "\\parbox[t]{0.75\\textwidth}{Note: Linear probability models. Variables beginning with 'Diff.' measure the support difference between two groups, calculated as (support of group A) - (support of group B).}")

# Our Table 2: Other Hypotheses are Null

stargazer(m10,m11, header=FALSE,
          omit.stat=c("f","ser","adj.rsq","AIC"),
          dep.var.caption = "DV: Policy adopted within 4 years of survey (1, 0)",
          dep.var.labels = "",
          column.labels=c("Model 3","Model 4"),
          title = "Association between preference difference between men and women and probability of policy adoption (gender gap in responsiveness)",
          column.sep.width = "10pt",  
          notes.align = "l",
          notes.label = "",
          notes = "\\parbox[t]{0.75\\textwidth}{Note: Linear probability models. Variables beginning with 'Diff.' measure the support difference between two groups, calculated as (support of group A) - (support of group B).}")


## Norway
d1 <- d %>% filter(country=="Norway")
m3 <- lm(outcome~all,data=d1)
m4 <- lm(outcome~all+genderdiff_nonabs,data=d1)

# Output for US
stargazer(m1,m2,m3,m4, header=FALSE,
          omit.stat=c("f","ser","adj.rsq","AIC"),
          dep.var.caption = "DV: Policy adopted within 4 years of survey (1, 0)",
          dep.var.labels = "",
          column.labels=c("US","US","Norway","Norway"),
          title = "Association between preference difference between men and women and probability of policy adoption (gender gap in responsiveness)",
          covariate.labels = c("Overall support",
                               "Diff. Men-Women"),
          column.sep.width = "10pt",  
          notes.align = "l",
          notes.label = "",
          notes = "\\parbox[t]{0.75\\textwidth}{Note: Linear probability models. Variables beginning with 'Diff.' measure the support difference between two groups, calculated as (support of group A) - (support of group B).}")


# Table 4: The gender gap in responsiveness when accounting for unequal responsiveness between income groups
# Models
## United States
d1 <- d %>% filter(country=="United States")
m1 <- lm(outcome~all+genderdiff_nonabs,data=d1)
m2 <- lm(outcome~all+inc9050_diff_nonabs+inc5010_diff_nonabs,data=d1)
m3 <- lm(outcome~all+genderdiff_nonabs+inc9050_diff_nonabs+inc5010_diff_nonabs,data=d1)
## Norway
d1 <- d %>% filter(country=="Norway")
d1$`Norway` <- d1$outcome
m4 <- lm(outcome~all+genderdiff_nonabs,data=d1)
m5 <- lm(outcome~all+inc9050_diff_nonabs+inc5010_diff_nonabs,data=d1)
m6 <- lm(outcome~all+genderdiff_nonabs+inc9050_diff_nonabs+inc5010_diff_nonabs,data=d1)
# Output
stargazer(m1,m2,m3,m4,m5,m6, header=FALSE, type='latex',
          omit.stat=c("f","ser","adj.rsq","AIC"),
          dep.var.caption = "DV: Policy adopted within 4 years of survey (1, 0)",
          dep.var.labels = "",
          column.labels=c("US","US","US","Norway","Norway","Norway"),
          title = "The gender gap in responsiveness when accounting for unequal responsiveness between income groups",
          covariate.labels = c("Overall support",
                               "Diff. Men-Women",
                               "Diff. P90-P50",
                               "Diff. P50-P10"),
          notes.align = "l",
          notes.label = "",
          notes = "\\parbox[t]{0.9\\textwidth}{Note: Linear probability models. Variables beginning with 'Diff.' measure the support difference between two groups, calculated as (support of group A) - (support of group B). P90, P50 and P10 represent the 90th, 50th and 10th income percentile (90th is the richest).}")


# Figure 3: Policy responsiveness to men and women when their preferences diverge by more than 10 percentage points in the US and Norway.
diff <- d %>% filter(genderdiff>0.10 & country=="United States")
m<-lm(outcome~male, data=diff)
newdata <- with(diff, data.frame(male=seq(0.05,0.95,by=0.01)))
p1 <- data.frame(pred=predict(m,newdata,type = "response", se.fit=T), labels= seq(0.05,0.95,by=0.01))
p1$group <- "Men"
m<-lm(outcome~female, data=diff)
newdata <- with(diff, data.frame(female=seq(0.05,0.95,by=0.01)))
p2 <- data.frame(pred=predict(m,newdata,type = "response", se.fit=T), labels= seq(0.05,0.95,by=0.01))
p2$group <- "Women"
p_us <- rbind(p1,p2)
p_us$group <- factor(p_us$group)
p_us$group <- factor(p_us$group,levels(p_us$group)[c(1,2)])
p_us$country <- "United States"
diff <- d %>% filter(genderdiff>0.10 & country=="Norway")
m<-lm(outcome~male, data=diff)
newdata <- with(diff, data.frame(male=seq(0.05,0.95,by=0.01)))
p1 <- data.frame(pred=predict(m,newdata,type = "response", se.fit=T), labels= seq(0.05,0.95,by=0.01))
p1$group <- "Men"
m<-lm(outcome~female, data=diff)
newdata <- with(diff, data.frame(female=seq(0.05,0.95,by=0.01)))
p2 <- data.frame(pred=predict(m,newdata,type = "response", se.fit=T), labels= seq(0.05,0.95,by=0.01))
p2$group <- "Women"
p_nor <- rbind(p1,p2)
p_nor$group <- factor(p_nor$group)
p_nor$group <- factor(p_nor$group,levels(p_nor$group)[c(1,2)])
p_nor$country <- "Norway"
plot <- rbind(p_us,p_nor)
plot$country <- factor(plot$country, levels(factor(plot$country))[c(2,1)])
ggplot(plot,aes(x=labels,y=pred.fit,color=group)) +
  geom_line(size=0.8) +
  scale_y_continuous(breaks=seq(0,1,0.2), labels=function(mean) paste0(mean*100,"%")) +
  scale_x_continuous(limits=c(0,1), breaks=seq(0,1,0.2), labels=function(labels) paste0(labels*100,"%")) +
  geom_text(data=subset(plot,labels==0.8), aes(label=group),vjust=c(-2.5,3,-2.5,3)) +
  labs(x="Percent favoring policy change",y="Predicted probability of policy change", 
       linetype=NULL, 
       color=NULL,
       title=NULL) +
  theme_bw() +
  scale_color_viridis_d(begin = 0,end=0.7) +
  coord_cartesian(ylim=c(0,0.9)) +
  theme(legend.position = "None",
        legend.background = element_rect(size=0.2, colour ="black"),
        text = element_text(size = 14),
        panel.grid = element_line(color="grey96"),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill="grey96")) +
  guides(linetype = guide_legend(override.aes = list(size = 0.4))) +
  facet_wrap(~country)


# Figure 4: Policy responsiveness to men and women by income percentile and country.
d1 <- d %>% filter( (genderdiff>0.1|inc9010_diff>0.1) & country=="United States")
tab1<-data.frame(pred_i10_men=t(d1 %>% summarise_at(vars(pred_i10_men),funs(eff_num,eff_se,p,sup20,sup80,fact,n))),
                 pred_i10_women=t(d1 %>% summarise_at(vars(pred_i10_women),funs(eff_num,eff_se,p,sup20,sup80,fact,n))))
d1 <- d %>% filter( (genderdiff>0.1|inc9010_diff>0.1) & country=="United States")
tab2<-data.frame(pred_i50_men=t(d1 %>% summarise_at(vars(pred_i50_men),funs(eff_num,eff_se,p,sup20,sup80,fact,n))),
                 pred_i50_women=t(d1 %>% summarise_at(vars(pred_i50_women),funs(eff_num,eff_se,p,sup20,sup80,fact,n))))
d1 <- d %>% filter( (genderdiff>0.1|inc9010_diff>0.1) & country=="United States")
tab3<-data.frame(pred_i90_men=t(d1 %>% summarise_at(vars(pred_i90_men),funs(eff_num,eff_se,p,sup20,sup80,fact,n))),
                 pred_i90_women=t(d1 %>% summarise_at(vars(pred_i90_women),funs(eff_num,eff_se,p,sup20,sup80,fact,n))))
tab_us0 <- cbind(tab1,tab2,tab3)
d1 <- d %>% filter( (genderdiff>0.1|inc9010_diff>0.1) & country=="Norway")
tab1<-data.frame(pred_i10_men=t(d1 %>% summarise_at(vars(pred_i10_men),funs(eff_num,eff_se,p,sup20,sup80,fact,n))),
                 pred_i10_women=t(d1 %>% summarise_at(vars(pred_i10_women),funs(eff_num,eff_se,p,sup20,sup80,fact,n))))
d1 <- d %>% filter( (genderdiff>0.1|inc9010_diff>0.1) & country=="Norway")
tab2<-data.frame(pred_i50_men=t(d1 %>% summarise_at(vars(pred_i50_men),funs(eff_num,eff_se,p,sup20,sup80,fact,n))),
                 pred_i50_women=t(d1 %>% summarise_at(vars(pred_i50_women),funs(eff_num,eff_se,p,sup20,sup80,fact,n))))
d1 <- d %>% filter( (genderdiff>0.1|inc9010_diff>0.1) & country=="Norway")
tab3<-data.frame(pred_i90_men=t(d1 %>% summarise_at(vars(pred_i90_men),funs(eff_num,eff_se,p,sup20,sup80,fact,n))),
                 pred_i90_women=t(d1 %>% summarise_at(vars(pred_i90_women),funs(eff_num,eff_se,p,sup20,sup80,fact,n))))
tab_no0 <- cbind(tab1,tab2,tab3)
tab_no <- tab_no0[-1,]
tab_us1 <- tab_us0
tab_no1 <- tab_no0
tab_us1$est <- row.names(tab_us1)
tab_us1$country <- "United States"
tab_no1$est <- row.names(tab_no1)
tab_no1$country <- "Norway"
tab <- rbind(tab_no1,tab_us1)
tab_long <- tab %>% gather(variable,value,pred_i10_men:pred_i90_women)
tab_long$gender <- ifelse(grepl("_men",tab_long$variable),"Men","Women")
tab_long$income <- ifelse(grepl("i10",tab_long$variable),"Low income",
                          ifelse(grepl("i50",tab_long$variable),"Median income","High income"))
tab_long$income <- factor(tab_long$income,levels(factor(tab_long$income))[c(2,3,1)])
tab_broad <- tab_long %>% spread(est,value)
tab_broad$country <- factor(tab_broad$country, levels(factor(tab_broad$country))[c(2,1)])
ggplot(tab_broad,aes(income,as.numeric(eff_num),fill=gender)) +
  geom_bar(width=0.8,stat="identity",position="dodge",color="black") +
  scale_y_continuous(limits=c(-0.05,1)) +
  scale_fill_manual(values=c("grey30","grey90")) +
  geom_text(aes(label=eff_se),
            position=position_dodge(width=0.8),
            vjust=-0.2,
            size=3) +
  geom_text(aes(label=ifelse(gender=="Men",as.character(income),NA),y=0.8),face="bold",fill="white",size=4) +
  theme_bw() + 
  theme(legend.position = "bottom",
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.grid = element_line(color="grey97"),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill="grey97")) +
  labs(x=NULL,y="Policy responsiveness (regression coefficient)",fill=NULL) +
  facet_wrap(~country,scales="free_y")


# Table 5: Gender gap in responsiveness conditioned by time and the share of women in parliament
d2 <- d %>% filter(country=="United States")

m1 <- lm(outcome~
           genderdiff_nonabs*YEAR+
           all_mean*YEAR,data=d2)

m2 <- lm(outcome~
           genderdiff_nonabs*women_share+
           all_mean*women_share,data=d2)

m3 <- lm(outcome~
           genderdiff_nonabs*YEAR+
           all_mean*YEAR+
           genderdiff_nonabs*women_share+
           all_mean*women_share,data=d2)

m4 <- lm(outcome~
           genderdiff_nonabs*YEAR+
           genderdiff_nonabs*women_share+
           all_mean*women_share+
           all_mean*YEAR+
           inc9050_diff_nonabs*YEAR+
           inc5010_diff_nonabs*YEAR+
           inc9050_diff_nonabs*women_share+
           inc5010_diff_nonabs*women_share,data=d2)

d2 <- d %>% filter(country=="Norway")

m5 <- lm(outcome~
           genderdiff_nonabs*YEAR+
           all_mean*YEAR,data=d2)

m6 <- lm(outcome~
           genderdiff_nonabs*women_share+
           all_mean*women_share,data=d2)

m7 <- lm(outcome~
           genderdiff_nonabs*YEAR+
           all_mean*YEAR+
           genderdiff_nonabs*women_share+
           all_mean*women_share,data=d2)

m8 <- lm(outcome~
           genderdiff_nonabs*YEAR+
           genderdiff_nonabs*women_share+
           all_mean*women_share+
           all_mean*YEAR+
           inc9050_diff_nonabs*YEAR+
           inc5010_diff_nonabs*YEAR+
           inc9050_diff_nonabs*women_share+
           inc5010_diff_nonabs*women_share,data=d2)

stargazer(m1,m2,m3,m5,m6,m7, header=FALSE, type='latex',
          omit.stat=c("f","ser","adj.rsq","AIC"),
          dep.var.caption = "DV: Policy adopted within 4 years of survey (1, 0)",
          dep.var.labels = "",
          column.labels=c("US","US","US","Norway","Norway","Norway"),
          title = "Gender gap in responsiveness conditioned by time and the share of women in parliament",
          covariate.labels = c("Diff. Men-Women",
                               "Year",
                               "Women MP\\%",
                               "Diff. Men-Women x Year",
                               "Diff. Men-Women x W. MP\\%"),
          omit = c("all_mean",
                   "YEAR:all_mean",
                   "women_share:all_mean",
                   "all_mean:women_share"),
          add.lines=list(c("Additional controls",
                           rep(" ",6)),
                         c("\\ \\ Overall sup. x Year",
                           c("\\ding{51}","","\\ding{51}","\\ding{51}","","\\ding{51}")),
                         c("\\ \\ Overall sup. x Women MP\\%",
                           c("","\\ding{51}","\\ding{51}","","\\ding{51}","\\ding{51}"))),
          column.sep.width = "-5pt",
          notes.align = "l",
          notes.label = "",
          notes = "\\parbox[t]{1\\textwidth}{Note: Linear probability models. Variables beginning with 'Diff.' measure the support difference between two groups, calculated as (support of group A) - (support of group B). Full regression results reported in Appendix Tables D1 and D2}")


# Figure 5: Gender gap in policy responsiveness as the share of women in parliament increases.
intmale<-interplot(m=m2,var1="genderdiff_nonabs",var2="women_share")$data
intmale$gender <- "United States"
intfemale<-interplot(m=m6,var1="genderdiff_nonabs",var2="women_share")$data
intfemale$gender <- "Norway"
int <- rbind(intmale,intfemale)
cols <- brewer_pal(palette="Set3")(10)
ggplot(int,aes(fake, coef1)) +
  geom_vline(xintercept = 0.5,color=cols[4],linetype="dashed",alpha=0.8) +
  geom_text(x=0.48,y=2,label="Gender parity in
            parliament",hjust=1,color=cols[4]) +
  geom_hline(yintercept=0,linetype=2,alpha=0.8,color=cols[5]) +
  geom_text(x=0.04,y=-0.2,label="Equal policy responsiveness",hjust=0,color=cols[5]) +
  geom_line() +
  geom_line(aes(fake,ub)) +
  geom_line(aes(fake,lb)) +
  theme_bw() +
  theme(panel.grid = element_line(color="grey97"),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill="grey97")) +
  facet_wrap(~gender,ncol=1)+
  labs(x="Share of women in parliament",y="Gender gap in responsiveness
       (regression coefficient)") +
  scale_x_continuous(limits=c(0,0.5),
                     breaks=seq(0,0.5,0.05),
                     labels=function(x)paste0(x*100,"%")) +
  scale_y_continuous(breaks=seq(-0.5,3,0.5))


#################### END  ###########################
